<?php echo "<?php defined('SYSPATH') or die('No direct script access.');"?>


class Generator_Controller<?php echo ($set_dir ? '_'.ucfirst($dir):'') ?>_<?php echo ucfirst($model) ?>  extends <?php echo Kohana::config('app.controller_parent') ?>
{
	public function action_index()
	{
		$<?php echo $model ?>_db = ORM::factory('<?php echo $model ?>');
		// Total number of users (reset FALSE prevents the query object from being cleared)
                $count = $<?php echo $model ?>_db
                        ->reset(FALSE)->count_all();

                $pagination = new Pagination(array(
			'items_per_page'	=> Arr::get($_GET, 'per_page', 10),
			'total_items'		=> $count,
		));

		$<?php echo $models?> = $<?php echo $model ?>_db
                                ->offset($pagination->offset)
                                ->limit($pagination->items_per_page)
                                ->find_all();

		$data = array(
			'<?php echo $models?>'	=> $<?php echo $models?>,
			'pagination'    => $pagination,
		);

                $this->template->title = '';
                $this->template->content = <?php echo Kohana::config('app.view_class') ?>::factory('<?php echo ($set_dir ? $dir.'/':'') ?><?php echo $model ?>/index', $data);
	}

	public function action_edit($id = null)
	{
            $<?php echo $model?> = ORM::factory('<?php echo $model ?>', $id);

            $form = $<?php echo $model?>->as_array();
            $errors = array();

            if ($_POST && $<?php echo $model?>->values($_POST)->check())
            {
                $<?php echo $model?>->save();
                Request::instance()->redirect('<?php echo ($set_dir ? $dir.'/':'') ?><?php echo $model?>/index');
                return;
            }

            $errors = $<?php echo $model?>->validate()->errors('<?php echo $model?>_edit');

            $data = array(
                    '<?php echo $model?>' => $<?php echo $model?>,
                    'errors' => $errors,
            );

            $this->template->title = 'Edit';
            $view_name = 'edit';
            if (! $<?php echo $model?>->loaded())
            {
                $this->template->title = 'New';
                $view_name = 'new';
            }

            $this->template->content = <?php echo Kohana::config('app.view_class') ?>::factory('<?php echo ($set_dir ? $dir.'/':'') ?><?php echo $model ?>/' . $view_name, $data);
	}

	public function action_action()
	{
            $<?php echo $model?>_ids = arr::get($_POST, '<?php echo $model?>_ids', array());
            $action = arr::get($_POST, 'action', 'default');

            foreach ($<?php echo $model?>_ids as $<?php echo $model?>_id)
            {
                $<?php echo $model?> = Sprig::factory('<?php echo $model?>');
                $<?php echo $model?>->load(DB::select()->where('id', '=', $<?php echo $model?>_id));

                if ( ! $<?php echo $model?>->loaded())
                        continue;

                switch ($action)
                {
                    case 'delete':
                        $<?php echo $model?>->delete();

                        flash::set('info', 'Объект удален - ');
                        break;
                }
            }

            Request::instance()->redirect(Request::$referrer);
	}
}